//
//  CheckInControl.m
//  WE
//
//  Created by iMacOne on 15/12/2.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "CheckInControl.h"
#import <Masonry.h>
#import "Size.h"
#import "EMMallSectionView.h"
#import "TitleAndArrowCell.h"
#import "DateUtil.h"
#import "SearchDisControl.h"
#import "TitleAndSwitchCell.h"
#import "AddTheContactViewController.h"
#import "QuickaboutDiagnosisofdiseaseTableViewCell.h"
#import "QuickaboutDiagnosisofdiseaseSternView.h"
#import "QuickaboutpPictureTableViewCell.h"
#define DATE_FORMAT @"yyyy-MM-dd"
static float gao;
@interface CheckInControl ()<UITextViewDelegate,QuickaboutDiagnosisofdiseaseSternDelehate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *array;
@property (nonatomic, strong)QuickaboutDiagnosisofdiseaseSternView *quicka;
@property (nonatomic, strong)QuickaboutDiagnosisofdiseaseTableViewCell *diagCelle;
@end

@implementation CheckInControl
-(void)viewWillAppear:(BOOL)animated
{
    //self.navigationController.navigationBar.hidden = NO;
    [self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    //使用NSNotificationCenter 鍵盤隐藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}
-(void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    gao = kbSize.height;
    NSLog(@"hight_hitht:%f",kbSize.height);
//    if (WindowHeight < 667) {
//        [_tableView setContentOffset:CGPointMake(0, gao+55) animated:YES];
//    }else{
//        [_tableView setContentOffset:CGPointMake(0, gao-26) animated:YES];
//    }
    [UIView animateWithDuration:0.1 animations:^
     {
         _quicka.frame = CGRectMake(0, WindowHeight-gao-40, WindowWidth, 40);
         _tableView.frame = CGRectMake(0, 0, WindowWidth, WindowHeight-gao-40);
         
     }];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self btnClick];
}
-(void)btnClick{
    NSRange range = NSMakeRange([_diagCelle.field.text length] - 1, 1);
    [_diagCelle.field scrollRangeToVisible:range];
    [_diagCelle endEditing:YES];
    _quicka.frame = CGRectMake(0, WindowHeight, WindowWidth, 40);
    _tableView.frame = CGRectMake(0, 0, WindowWidth, WindowHeight);
}
-(void)lefts{
    
}
-(void)rights{
    
}
- (void)textViewDidChange:(UITextView *)textView;

{
    if ([ToolUtil isBlank:textView.text]) {
        _diagCelle.label.hidden = NO;
    }else {
        _diagCelle.label.hidden = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"患者报到单";
    self.array = @[@"患者",@"就诊日期",@"确定疾病"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"提交" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    rightBtn.titleLabel.font = FONT(12);
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
   
    self.navigationItem.rightBarButtonItem = rightItem;
    [self.view addSubview:self.tableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    [self setExtraCellLineHidden:self.tableView];

    _quicka = [[QuickaboutDiagnosisofdiseaseSternView alloc] initWithFrame:CGRectMake(0, WindowHeight, WindowWidth, 40)];
    _quicka.delegate = self;
    [self.view addSubview:_quicka];
}
- (void)creatView
{
    NSArray * array = @[@"患者",@"就诊日期",@"确定疾病"];
    for (int i = 0; i < 3; i++) {
        
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
        
        lbl.text = array[i];
        lbl.font = FONT(12);
        lbl.textColor = [UIColor grayColor];
        [self.view addSubview:lbl];

    }

}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,WindowWidth , WindowHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    EMMallSectionView *sectionView =  [EMMallSectionView showWithName:nil];
    sectionView.tableView = self.tableView;
    sectionView.section = section;
    sectionView.sign = @"1";
    return sectionView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 15;
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 2;
    }
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        TitleAndArrowCell * cell = [[TitleAndArrowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.titleLbl.text = self.array[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.conLbl.text = @"haha";
        return cell;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            _diagCelle = [tableView dequeueReusableCellWithIdentifier:@"checkin"];
            if (_diagCelle == nil) {
                _diagCelle = [QuickaboutDiagnosisofdiseaseTableViewCell new];
            }
            _diagCelle.field.delegate = self;
            _diagCelle.label.text = @"请描述您的病情和治疗经过";
            return _diagCelle;
        }else if (indexPath.row == 1){
            QuickaboutpPictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"security"];
            if (cell == nil) {
                cell = [QuickaboutpPictureTableViewCell new];
            }
            cell.imgLabel.text = @"添加图片(不超过9张)";
            cell.place.text = @"病历或检查图片，方便医生确认";
            return cell;
        }
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

//                cell.conLbl.text = @"haha";
        return cell;
    }
    
    static NSString * cellIdentifier = @"cell8";
    TitleAndSwitchCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[TitleAndSwitchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.switchBtn.tag = 200 + indexPath.row + indexPath.section * 10;
     
    [cell.switchBtn addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray * array = tableView.visibleCells;
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                [self btnClick];
                UIDatePicker * pick = [self.view viewWithTag:300];
                UIView * aView = [self.view viewWithTag:230];
                [pick removeFromSuperview];
                [aView removeFromSuperview];
                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:(UIAlertControllerStyleActionSheet)];
                
                UIAlertAction * addAction = [UIAlertAction actionWithTitle:@"新增患者" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    [self.navigationController pushViewController:[[AddTheContactViewController alloc] init] animated:YES];
//                    TitleAndArrowCell * cell = array[0];
//                    cell.conLbl.text = @"haha";
                        }];
                UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                }];
                [alertController addAction:addAction];
                [alertController addAction:cancelAction];

                [self presentViewController:alertController animated:YES completion:^{
                    
                }];
            }
              if (indexPath.row == 1) {
                  [self btnClick];
                  UIDatePicker * datePicker = [UIDatePicker new];
                  datePicker.datePickerMode = UIDatePickerModeDate;
                  datePicker.tag = 300;
                  NSDate* minDate = [DateUtil getDate:@"1990-01-01"];
                  NSDate* maxDate = [DateUtil getDate:@"2099-01-01"];
                  datePicker.minimumDate = minDate;
                  datePicker.maximumDate = maxDate;
                  datePicker.backgroundColor = GRAYCOLOR;
                  
                  [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
                  
                  [self.view addSubview:datePicker];
                  [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
                      make.bottom.equalTo(self.view.mas_bottom);
                      make.width.offset(WindowWidth);
                      make.left.offset(0);
                  }];
                  UIView * aView = [UIView new];
                  aView.backgroundColor = GRAYCOLOR;
                  aView.tag = 230;
                  [self.view addSubview:aView];
                  [aView mas_makeConstraints:^(MASConstraintMaker *make) {
                      make.bottom.equalTo(datePicker.mas_top);
                      make.left.offset(0);
                      make.height.offset(20);
                      make.width.offset(WindowWidth);
                  }];
                  UIButton * btn = [UIButton new];
                  [aView addSubview:btn];
                  btn.backgroundColor = GRAYCOLOR;
                  [btn setTitle:@"确定" forState:UIControlStateNormal];
                  [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                  [btn addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
                  btn.titleLabel.font = FONT(14);
                  btn.tag = 210;
                  [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                      make.top.offset(0);
                      make.right.equalTo(datePicker.mas_right).with.offset(-20);
//                      make.centerX.equalTo(self.view);
                      make.height.offset(20);
                  }];
                  UIButton * cancelBtn = [UIButton new];
                  [aView addSubview:cancelBtn];
                  cancelBtn.backgroundColor = GRAYCOLOR;
                  [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
                  [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                  [cancelBtn addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
                  cancelBtn.titleLabel.font = FONT(14);
                  cancelBtn.tag = 220;
                  [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                      make.top.offset(0);
                      make.left.equalTo(aView).with.offset(20);
                      //                      make.centerX.equalTo(self.view);
                      make.height.offset(20);
                  }];
                  
            }
            if (indexPath.row == 2) {
                [self btnClick];
                [self.navigationController pushViewController:[[SearchDisControl alloc] init] animated:YES];
                UIDatePicker * pick = [self.view viewWithTag:300];
                UIView * aView = [self.view viewWithTag:230];
                [pick removeFromSuperview];
                [aView removeFromSuperview];
            }
            
            break;
            case 1:
            if (indexPath.row == 0 || indexPath.row == 1) {
                UIDatePicker * pick = [self.view viewWithTag:300];
                UIView * aView = [self.view viewWithTag:230];
                [ pick removeFromSuperview];
                [aView removeFromSuperview];
            }
            
            break;
            
            
            
        default:
            break;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}
- (void)doClick:(UIButton *)btn
{
    NSArray * array = [self.tableView visibleCells];
    
    UIDatePicker * pick = [self.view viewWithTag:300];
    UIView * aView = [self.view viewWithTag:230];

    if (btn.tag == 210) {
        TitleAndArrowCell * cell = array[1];
        [pick removeFromSuperview];
        [aView removeFromSuperview];
        cell.conLbl.text = [DateUtil getString:pick.date :DATE_FORMAT];

    }else if (btn.tag == 220){
        [pick removeFromSuperview];
        [aView removeFromSuperview];

    }
}
- (void)changeSwitch:(UISwitch *)switchBtn
{
    if (switchBtn.tag == 220) {
        if (switchBtn.isOn) {
            NSLog(@"同意");
        }else{
             NSLog(@"不同意");
        }
    }
}

- (void)dateChange:(UIDatePicker *)picker
{
    NSLog(@"%@",picker.date);
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 160;
        }
        return 100;
    }
    return 40;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}



@end
